package com.shopx.gen.service.impl;

import com.shopx.gen.dao.db1.GenDao;
import com.shopx.gen.entity.ClassInfo;
import com.shopx.gen.service.IGenTableService;
import com.shopx.gen.utils.CodeGeneratorTool;
import com.shopx.gen.utils.FileUtil;
import com.shopx.gen.utils.FreemarkerTool;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lituo
 * @className: GenTableServiceImpl
 * @description: TODO
 * @date 2021/12/4 3:45 PM
 */
@Service
@Slf4j
public class GenTableServiceImpl implements IGenTableService {
  @Resource
  private GenDao genDao;

  @Value("${genSql.authorName}")
  private String authorName;

  @Value("${genSql.packageName}")
  private String packageName;

  @Value("${genSql.tabInfo}")
  private String tabInfo;

  @Value("${genSql.filePath}")
  private String filePath;

  @Resource
  private FreemarkerTool freemarkerTool;


  public String queryGenTabSql(String tableName) {
    log.info("queryGenTabSql.begin:{}", tableName);
    Map<String, String> tabSqlMap = genDao.queryGenTabSql(tableName);
    String createTabSql = tabSqlMap.get("Create Table");
    log.info("createTabSql:{}", createTabSql);
    return createTabSql;
  }

  public void genFile() throws Exception {
    String projectPath = filePath;
    if (StringUtils.isEmpty(authorName)) {
      throw new RuntimeException("作者不能为空");
    }
    if (StringUtils.isEmpty(packageName)) {
      throw new RuntimeException("包名不能为空");
    }
    if (StringUtils.isEmpty(tabInfo)) {
      throw new RuntimeException("表信息不能为空");
    }
    for (String tab : tabInfo.split("@")) {
      if (StringUtils.isEmpty(tab)) {
        continue;
      }
      String tabName = tab;
      List<Map<String,String>> primaryKeyList = genDao.queryTabPriKey(tabName);
      String primaryKey = "";
      if (StringUtils.isEmpty(tabName) || primaryKeyList == null || primaryKeyList.size()==0) {
        continue;
      }
      for(Map<String,String> map :primaryKeyList ){
        primaryKey+=map.get("COLUMN_NAME")+",";
      }

      String tableSql = this.queryGenTabSql(tabName);
      ClassInfo classInfo = CodeGeneratorTool.processTableIntoClassInfo(tableSql, primaryKey.substring(0,primaryKey.length()-1));

      Map<String, Object> params = new HashMap<String, Object>();
      params.put("classInfo", classInfo);
      params.put("authorName", authorName);
      params.put("packageName", packageName);

      Map<String, String> result = new HashMap<String, String>();
      String controller = freemarkerTool.processString("controller.ftl", params);
      result.put("controller", controller);
      log.info("controller:{}", controller);
      FileUtil.createFile(projectPath + File.separator + "contoller" + File.separator, classInfo.getClassName() + "Controller.java", controller);

      String service = freemarkerTool.processString("service.ftl", params);
      result.put("service", service);
      log.info("service:{}", service);
      FileUtil.createFile(projectPath + File.separator + "service" + File.separator, "I" + classInfo.getClassName() + "Service.java", service);

      String serviceImpl = freemarkerTool
          .processString("service_impl.ftl", params);
      result.put("service_impl", serviceImpl);
      log.info("serviceImpl:{}", serviceImpl);
      FileUtil.createFile(projectPath + File.separator + "service" + File.separator, classInfo.getClassName() + "ServiceImpl.java", serviceImpl);


      String dao = freemarkerTool.processString("dao.ftl", params);
      result.put("dao", dao);
      log.info("dao:{}", dao);
      FileUtil.createFile(projectPath + File.separator + "dao" + File.separator, classInfo.getClassName() + "Dao.java", dao);

      String mybatis = freemarkerTool.processString("mybatis.ftl", params);
      result.put("mybatis", mybatis);
      log.info("mybatis:{}", mybatis);
      FileUtil.createFile(projectPath + File.separator + "mapper" + File.separator, classInfo.getClassName() + "Mapper.xml", mybatis);

      String bean = freemarkerTool.processString("bean.ftl", params);
      result.put("bean", bean);
      log.info("bean:{}", bean);
      FileUtil.createFile(projectPath + File.separator + "bean" + File.separator, classInfo.getClassName() + ".java", bean);

      String model = freemarkerTool.processString("model.ftl", params);
      result.put("model", model);
      log.info("model:{}", model);
      FileUtil.createFile(projectPath + File.separator + "model" + File.separator, classInfo.getClassName() + "Model.java", model);
    }
  }
}
